home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / FDOPEN.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  2KB  |  83 lines

  1. /* This is file FDOPEN.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /*
  8.  * Copyright (c) 1980 Regents of the University of California.
  9.  * All rights reserved.  The Berkeley software License Agreement
  10.  * specifies the terms and conditions for redistribution.
  11.  */
  12.  
  13. #if defined(LIBC_SCCS) && !defined(lint)
  14. static char sccsid[] = "@(#)fdopen.c    5.2 (Berkeley) 3/9/86";
  15. #endif LIBC_SCCS and not lint
  16.  
  17. /*
  18.  * Unix routine to do an "fopen" on file descriptor
  19.  * The mode has to be repeated because you can't query its
  20.  * status
  21.  */
  22.  
  23. #include <sys/types.h>
  24. #include <sys/file.h>
  25. #include <stdio.h>
  26.  
  27. FILE *
  28. fdopen(fd, mode)
  29.     const char *mode;
  30. {
  31.     extern FILE *_findiop();
  32.     static int nofile = -1;
  33.     register FILE *iop;
  34.     int new_mode = _fmode;
  35.  
  36.     if (nofile < 0)
  37.         nofile = getdtablesize();
  38.  
  39.     if (fd < 0 || fd >= nofile)
  40.         return (NULL);
  41.  
  42.     iop = _findiop();
  43.     if (iop == NULL)
  44.         return (NULL);
  45.  
  46.     iop->_cnt = 0;
  47.     iop->_file = fd;
  48.     iop->_bufsiz = 0;
  49.     iop->_base = iop->_ptr = NULL;
  50.  
  51.     switch (*mode) {
  52.     case 'r':
  53.         iop->_flag = _IOREAD;
  54.         break;
  55.     case 'a':
  56.         lseek(fd, (off_t)0, L_XTND);
  57.         /* fall into ... */
  58.     case 'w':
  59.         iop->_flag = _IOWRT;
  60.         break;
  61.     default:
  62.         return (NULL);
  63.     }
  64.  
  65.     if (mode[1] == '+')
  66.     {
  67.         iop->_flag = _IORW;
  68.         mode++;
  69.     }
  70.     if (mode[1] == 't')
  71.     {
  72.         iop->_flag |= _IOTEXT;
  73.         new_mode = O_TEXT;
  74.     }
  75.     if (mode[1] == 'b')
  76.     {
  77.         new_mode = O_BINARY;
  78.     }
  79.     setmode(fd, new_mode);
  80.  
  81.     return (iop);
  82. }
  83.